package dao

import (
	"context"
	"mall/internal/model"
)

func (d *Dao) FindCartProds(ctx context.Context, memberId uint64) (
	cartProds []*model.Cart, err error) {
	if err = d.TransContext(ctx).Model(&model.Cart{}).Where(
		"member_id = ?", memberId).Order("created_at asc").Scan(&cartProds).Error; err != nil {
		err = d.processSqlError(err)
		return
	}
	return
}

func (d *Dao) GetCartProd(ctx context.Context, memberId, skuId uint64) (
	cartProd *model.Cart, err error) {
	cartProd = &model.Cart{}
	if err = d.TransContext(ctx).Model(&model.Cart{}).Where(
		"member_id = ? AND sku_id = ?",
		memberId, skuId).Take(cartProd).Error; err != nil {
		err = d.processSqlError(err)
		return
	}
	return
}

func (d *Dao) DeleteCartBySkuIds(ctx context.Context, memberId uint64, skuIds []uint64) (err error) {
	if err = d.TransContext(ctx).Delete(&model.Cart{},
		"member_id = ? AND sku_id in ?",
		memberId, skuIds).Error; err != nil {
		err = d.processSqlError(err)
		return
	}
	return
}

func (d *Dao) DeleteCartByMember(ctx context.Context, memberId uint64) (err error) {
	if err = d.TransContext(ctx).Delete(&model.Cart{},
		"member_id = ?",
		memberId).Error; err != nil {
		err = d.processSqlError(err)
		return
	}
	return
}
